package com.fatesgo.controller;

import com.fatesgo.mapper.ClassroomMapper;
import com.fatesgo.pojo.Classroom;
import com.fatesgo.util.Response;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/classroom")
public class ClassroomController {
    @Autowired
    private HttpServletRequest request;

    @Autowired(required = false)
    private ClassroomMapper classroomMapper;

    @GetMapping("/getClassroomInfo/{id}")
    public Response getClassroomInfoById(@PathVariable("id") int id) {
        Classroom classroom = classroomMapper.getClassroomInfoById(id);
        return Response.success("成功", classroom);
    }

    @PutMapping("/updateClassroomInfo")
    public Response updateClassroomInfo(@RequestBody Classroom classroom) {
        int row = classroomMapper.updateClassroomInfo(classroom);
        return Response.success("成功", row);
    }

        @PostMapping("/createClassroom")
    public Response createClassroom(@RequestBody Classroom classroom) {
        int row = classroomMapper.createClassroom(classroom);
        return Response.success("成功", row);
    }

    @DeleteMapping("/{id}")
    public Response deleteClassroom(@PathVariable("id") int id) {
        int row = classroomMapper.deleteClassroom(id);
        return Response.success("删除成功", row);
    }

    @GetMapping("/list")
    public Response list(Classroom classroom, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Classroom> classrooms = classroomMapper.selectAll(classroom);
        return Response.page(classrooms, new PageInfo<>(classrooms).getTotal());
    }
}
